package dame1;

import java.lang.reflect.Array;
import java.util.Arrays;

public class myArrayList implements IList{
    public int[] array;
    public  int usedsize;
    public static final int DEFAULT_CAPAITY = 10;
    public myArrayList(){
        this.array = new int[DEFAULT_CAPAITY ];
    }

    @Override
    public boolean isFill() {
        return this.usedsize==array.length;
    }
    @Override
    public void add(int data) {
        if (isFill()){
            grow();
        }
        this.array[this.usedsize] = data;
        this.usedsize++;
    }
    private void grow(){//扩容
        this.array  = Arrays.copyOf(this.array,2*this.array.length);
    }
    private void chickpos(int pos) throws posIlleagl {
        if (pos<0||pos>usedsize){
            throw new posIlleagl("pos位置不合法");
        }
    }
    @Override
    public void add(int pos, int data) {
        try {
            chickpos(pos);
        }catch ( posIlleagl e){
            System.out.println("插入元素pos位置不合法");
            e.printStackTrace();
        }
        if (isFill()){
            grow();
        }//下一步挪动元素
        for (int i = usedsize-1;i>=pos;i--){
            this.array[i+1]=array[i];
        }
        this.array[pos]=data;
        usedsize++;
    }

    @Override
    public boolean contains(int toFind) {//判断查找的值是否存在
        for(int i=0;i< array.length;i++){
            if (toFind == array[i]){
                return true;
            }
        }
        return false;
    }

    @Override
    public int indexOf(int toFind) {  //判断查找的值是否存在，存在返回下标
        for(int i=0;i< array.length;i++){
            if (toFind == array[i]){
                return i;
            }
        }
        return -1;
    }
    private void chickpos2(int pos) throws posIlleagl {
        if (pos<0||pos>=usedsize){
            throw new posIlleagl("pos位置不合法");
        }
    }
    private boolean isEmpy(){  //判断数组是否为空
        return usedsize==0;
    }
    private void checkempty(){
        if (isEmpy()){
            throw  new Empty("顺序表为空");
        }
    }
    public int get(int pos) throws Empty,posIlleagl {
        try {
            checkempty();
            chickpos2(pos);
            return array[pos];
        }catch (posIlleagl e){
            e.printStackTrace();
        }catch (Empty e){
            e.printStackTrace();
        }
        return-1;
    }

    @Override
    public void set(int pos, int value) {
        try {
            checkempty();
            chickpos2(pos);
            array[pos] = value;
        }catch (posIlleagl e){
            e.printStackTrace();
        }catch (Empty e){
            e.printStackTrace();
        }
    }
    @Override
    public void remove(int toRemove) {
        try {
            checkempty();
            int pos = indexOf(toRemove);
            if (pos==-1)return;
            for (int i=pos;i<usedsize-1;i++){
                array[i] = array[i+1];
            }
            usedsize--;
        } catch (Empty e){
            e.printStackTrace();
        }
    }

    @Override
    public int size() {
        return this.usedsize;
    }

    @Override
    public void clear() {
        usedsize = 0;
    }

    @Override
    public void display() {
        for (int i=0;i<this.usedsize;i++){
            System.out.println(this.array[i]+" ");
        }
    }
}
